package com.xzr.spring;

import com.xzr.test.Person;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
    static Map<String, Person> map = new HashMap<>();

    static {
        //1.读取配置文件
        Properties properties = new Properties();
        InputStream resourceAsStream = BeanFactory.class.getResourceAsStream("beans.properties");
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (String key : properties.stringPropertyNames()) {
            String property = properties.getProperty(key);

            Person person = null;
            try {
                person = (Person) Class.forName(property).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            map.put(key, person);


        }
    }

    public Person getBean(String key) {

        return map.get(key);
    }

}
